/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mbean.mobile;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import model.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import service.IUserService;

/**
 *
 * @author dingnangen
 */
@Named(value = "mobileLogin")
@SessionScoped
public class MobileLogin implements Serializable {

    @EJB(beanName = "UserManager")
    private IUserService<User> userManager;
    int i = 0;
    Subject subject;

    public MobileLogin() {
    }

    public boolean MobileLogin(String username, String password) {

        UsernamePasswordToken token;

        boolean loginsuccess;

        try {

            username = username.trim();
            password = password.trim();
            token = new UsernamePasswordToken(username, password);

            subject = SecurityUtils.getSubject();
            subject.login(token);
            System.out.println("loginsuccess" + i++);
            loginsuccess = true;

        } catch (Exception e) {
            System.out.println("loginfail" + i++);
            loginsuccess = false;

        }
        return loginsuccess;
    }

    public List findRoleByUserLoginName(String loginName) {
        return userManager.findRoleByUserLoginName(loginName);
    }

    public Subject getSubject() {
        return subject;
    }

    public void setSubject(Subject subject) {
        this.subject = subject;
    }

}
